package com.wwk.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.wwk.dao.PermissionDao;
import com.wwk.dao.RoleDao;
import com.wwk.dao.UserDao;
import com.wwk.pojo.Permission;
import com.wwk.pojo.Role;
import com.wwk.pojo.User;
import com.wwk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @Classname UserServiceImpl
 * @Description TODO
 * @Date 2020/11/12 9:24
 * @Created by wwk
 * @Author wwk
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findUserByname(String username) {
        User user = userDao.findUserByname(username);
        if(user==null){
            return null;
        }
        final Integer id = user.getId();
        //根据用户id查询角色集合
        final Set<Role> byUserId = roleDao.getByUserId(id);
        //遍历角色，为每个角色添加响应权限集合
        for (Role role:byUserId) {
            final Integer id1 = role.getId();
            //为每个角色关联权限
            final Set<Permission> byRoleId = permissionDao.findByRoleId(id1);
            role.setPermissions(byRoleId);
        }
        //用户关联角色
        user.setRoles(byUserId);
        return user;

    }
}
